<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- cpu-usage-analyzer.qdoc -->
  <title>Analyzing CPU Usage | Qt Creator Manual</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt Creator Manual</a></td><td >Analyzing CPU Usage</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="index.html">Qt Creator Manual 4.11.1</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="creator-heob.html" />
  <link rel="next" href="creator-cppcheck.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="creator-heob.html">Detecting Memory Leaks with Heob</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="creator-cppcheck.html">Analyzing Code with Cppcheck</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#using-the-performance-analyzer">Using the Performance Analyzer</a></li>
<li class="level1"><a href="#profiling-memory-usage-on-devices">Profiling Memory Usage on Devices</a></li>
<li class="level1"><a href="#specifying-performance-analyzer-settings">Specifying Performance Analyzer Settings</a></li>
<li class="level2"><a href="#choosing-event-types">Choosing Event Types</a></li>
<li class="level2"><a href="#choosing-a-sampling-mode-and-period">Choosing a Sampling Mode and Period</a></li>
<li class="level2"><a href="#selecting-call-graph-mode">Selecting Call Graph Mode</a></li>
<li class="level2"><a href="#setting-stack-snapshot-size">Setting Stack Snapshot Size</a></li>
<li class="level2"><a href="#adding-command-line-options-for-perf">Adding Command Line Options For Perf</a></li>
<li class="level2"><a href="#resolving-names-for-jit-compiled-javascript-functions">Resolving Names for JIT-compiled JavaScript Functions</a></li>
<li class="level1"><a href="#analyzing-collected-data">Analyzing Collected Data</a></li>
<li class="level2"><a href="#selecting-event-ranges">Selecting Event Ranges</a></li>
<li class="level2"><a href="#understanding-the-data">Understanding the Data</a></li>
<li class="level1"><a href="#viewing-statistics">Viewing Statistics</a></li>
<li class="level2"><a href="#visualizing-statistics-as-flame-graphs">Visualizing Statistics as Flame Graphs</a></li>
<li class="level2"><a href="#interaction-between-the-views">Interaction between the views</a></li>
<li class="level1"><a href="#loading-perf-data-files">Loading Perf Data Files</a></li>
<li class="level1"><a href="#loading-and-saving-trace-files">Loading and Saving Trace Files</a></li>
<li class="level1"><a href="#troubleshooting">Troubleshooting</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Analyzing CPU Usage</h1>
<span class="subtitle"></span>
<!-- $$$creator-cpu-usage-analyzer.html-description -->
<div class="descr"> <a name="details"></a>
<p>Qt Creator is integrated with the Linux Perf tool that can be used to analyze the CPU and memory usage of an application on embedded devices and, to a limited extent, on Linux desktop platforms. The Performance Analyzer uses the Perf tool bundled with the Linux kernel to take periodic snapshots of the call chain of an application and visualizes them in a timeline view or as a flame graph.</p>
<a name="using-the-performance-analyzer"></a>
<h2 id="using-the-performance-analyzer">Using the Performance Analyzer</h2>
<p>The Performance Analyzer usually needs to be able to locate debug symbols for the binaries involved.</p>
<p>Profile builds produce optimized binaries with separate debug symbols and should generally be used for profiling.</p>
<p>To manually set up a build configuration to provide separate debug symbols, edit the project build settings:</p>
<ol class="1" type="1"><li>To generate debug symbols also for applications compiled in release mode, select <b>Projects</b>, and then select <b>Details</b> next to <b>Build Steps</b> to view the build steps.</li>
<li>Select the <b>Generate separate debug info</b> check box.</li>
<li>Select <b>Yes</b> to recompile the project.</li>
</ol>
<p>You can start the Performance Analyzer in the following ways:</p>
<ul>
<li>Select <b>Analyze</b> &gt; <b>Performance Analyzer</b> to profile the current application.</li>
<li>Select the <img src="images/qtcreator-analyze-start-button.png" alt="" /> (<b>Start</b>) button to start the application from the Performance Analyzer.</li>
</ul>
<p><b>Note: </b>If data collection does not start automatically, select the <img src="images/recordfill.png" alt="" /> (<b>Collect profile data</b>) button.</p><p>When you start analyzing an application, the application is launched, and the Performance Analyzer immediately begins to collect data. This is indicated by the time running in the <b>Recorded</b> field. However, as the data is passed through the Perf tool and an extra helper program bundled with Qt Creator, and both buffer and process it on the fly, data may arrive in Qt Creator several seconds after it was generated. An estimate for this delay is given in the <b>Processing delay</b> field.</p>
<p>Data is collected until you select the <b>Stop collecting profile data</b> button or terminate the application.</p>
<p>Select the <b>Stop collecting profile data</b> button to disable the automatic start of the data collection when an application is launched. Profile data will still be generated, but Qt Creator will discard it until you select the button again.</p>
<a name="profiling-memory-usage-on-devices"></a>
<h2 id="profiling-memory-usage-on-devices">Profiling Memory Usage on Devices</h2>
<p>To create trace points for profiling memory usage on a target device, select <b>Analyze</b> &gt; <b>Performance Analyzer Options</b> &gt; <b>Create Memory Trace Points</b>.</p>
<p>To add events for the trace points, see <a href="creator-cpu-usage-analyzer.html#choosing-event-types">Choosing Event Types</a></p>
<p>You can record a memory trace to view usage graphs in the samples rows of the timeline and to view memory allocations, peaks, and releases in the flame graph.</p>
<a name="specifying-performance-analyzer-settings"></a>
<h2 id="specifying-performance-analyzer-settings">Specifying Performance Analyzer Settings</h2>
<p>To specify global settings for the Performance Analyzer, select <b>Tools</b> &gt; <b>Options</b> &gt; <b>Analyzer</b> &gt; <b>CPU Usage</b>. For each run configuration, you can also use specialized settings. Select <b>Projects</b> &gt; <b>Run</b>, and then select <b>Details</b> next to <b>Performance Analyzer Settings</b>.</p>
<p class="centerAlign"><img src="images/qtcreator-performance-analyzer-settings.png" alt="" /></p><p>To edit the settings for the current run configuration, you can also select the dropdown menu next to the <b>Collect profile data</b> button.</p>
<a name="choosing-event-types"></a>
<h3 id="choosing-event-types">Choosing Event Types</h3>
<p>In the <b>Events</b> table, you can specify which events should trigger the Performance Analyzer to take a sample. The most common way of analyzing CPU usage involves periodic sampling, driven by hardware performance counters that react to the number of instructions or CPU cycles executed. Alternatively, a software counter that uses the CPU clock can be chosen.</p>
<p>Select <b>Add Event</b> to add events to the table. In the <b>Event Type</b> column, you can choose the general type of event to be sampled, most commonly <b>hardware</b> or <b>software</b>. In the <b>Counter</b> column, you can choose which specific counter should be used for the sampling. For example, <b>instructions</b> in the <b>hardware</b> group or <b>cpu-clock</b> in the <b>software</b> group.</p>
<p>More specialized sampling, for example by cache misses or cache hits, is possible. However, support for it depends on specific features of the CPU involved. For those specialized events, you can give more detailed sampling instructions in the <b>Operation</b> and <b>Result</b> columns. For example, you can choose a <b>cache</b> event for <b>L1-dcache</b> on the <b>load</b> operation with a result of <b>misses</b>. That would sample L1-dcache misses on reading.</p>
<p>Select <b>Remove Event</b> to remove the selected event from the table.</p>
<p>Select <b>Use Trace Points</b> to replace the current selection of events with trace points defined on the target device and set the <b>Sample mode</b> to <b>event count</b> and the <b>Sample period</b> to <code>1</code>. If the trace points on the target were defined using the <b>Create Trace Points</b> option, the Performance Analyzer will automatically use them to profile memory usage.</p>
<p>Select <b>Reset</b> to revert the selection of events, as well as the <b>Sample mode</b> and <b>Sample period</b> to the default values.</p>
<a name="choosing-a-sampling-mode-and-period"></a>
<h3 id="choosing-a-sampling-mode-and-period">Choosing a Sampling Mode and Period</h3>
<p>In the <b>Sample mode</b> and <b>Sample period</b> fields, you can specify how samples are triggered:</p>
<ul>
<li>Sampling by <b>event count</b> instructs the kernel to take a sample every <code>n</code> times one of the chosen events has occurred, where <code>n</code> is specified in the <b>Sample period</b> field.</li>
<li>Sampling by <b>frequency (Hz)</b> instructs the kernel to try and take a sample <code>n</code> times per second, by automatically adjusting the sampling period. Specify <code>n</code> in the <b>Sample period</b> field.</li>
</ul>
<p>High frequencies or low event counts result in more accurate data, at the expense of a higher overhead and a larger volume of data being generated. The actual sampling period is determined by the Linux kernel on the target device, which takes the period set for Perf merely as advice. There may be a significant difference between the sampling period you request and the actual result.</p>
<p>In general, if you configure the Performance Analyzer to collect more data than it can transmit over the connection between the target and the host device, the application may get blocked while Perf is trying to send the data, and the processing delay may grow excessively. You should then change the <b>Sample period</b> or the <b>Stack snapshot size</b>.</p>
<a name="selecting-call-graph-mode"></a>
<h3 id="selecting-call-graph-mode">Selecting Call Graph Mode</h3>
<p>In the <b>Call graph mode</b> field, you can specify how the Performance Analyzer recovers call chains from your application:</p>
<ul>
<li>The <b>Frame Pointer</b>, or <code>fp</code>, mode relies on frame pointers being available in the profiled application and will instruct the kernel on the target device to walk the chain of frame pointers in order to retrieve a call chain for each sample.</li>
<li>The <b>Dwarf</b> mode works also without frame pointers, but generates significantly more data. It takes a snapshot of the current application stack each time a sample is triggered and transmits that snapshot to the host computer for analysis.</li>
<li>The <b>Last Branch Record</b> mode does not use a memory buffer. It automatically decodes the last 16 taken branches every time execution stops. It is supported only on recent Intel CPUs.</li>
</ul>
<p>Qt and most system libraries are compiled without frame pointers by default, so the frame pointer mode is only useful with customized systems.</p>
<a name="setting-stack-snapshot-size"></a>
<h3 id="setting-stack-snapshot-size">Setting Stack Snapshot Size</h3>
<p>The Performance Analyzer will analyze and <i>unwind</i> the stack snapshots generated by Perf in dwarf mode. Set the size of the stack snapshots in the <b>Stack snapshot size</b> field. Large stack snapshots result in a larger volume of data to be transferred and processed. Small stack snapshots may fail to capture call chains of highly recursive applications or other intense stack usage.</p>
<a name="adding-command-line-options-for-perf"></a>
<h3 id="adding-command-line-options-for-perf">Adding Command Line Options For Perf</h3>
<p>You can specify additional command line options to be passed to Perf when recording data in the <b>Additional arguments</b> field. You may want to specify <code>--no-delay</code> or <code>--no-buffering</code> to reduce the processing delay. However, those options are not supported by all versions of Perf and Perf may not start if an unsupported option is given.</p>
<a name="resolving-names-for-jit-compiled-javascript-functions"></a>
<h3 id="resolving-names-for-jit-compiled-javascript-functions">Resolving Names for JIT-compiled JavaScript Functions</h3>
<p>Since version 5.6&#x2e;0, Qt can generate perf.map files with information about JavaScript functions. The Performance Analyzer will read them and show the function names in the <b>Timeline</b>, <b>Statistics</b>, and <b>Flame Graph</b> views. This only works if the process being profiled is running on the host computer, not on the target device. To switch on the generation of perf.map files, add the environment variable <code>QV4_PROFILE_WRITE_PERF_MAP</code> to the <b>Run Environment</b> and set its value to <code>1</code>.</p>
<a name="analyzing-collected-data"></a>
<h2 id="analyzing-collected-data">Analyzing Collected Data</h2>
<p>The <b>Timeline</b> view displays a graphical representation of CPU usage per thread and a condensed view of all recorded events.</p>
<p class="centerAlign"><img src="images/qtcreator-performance-analyzer-timeline.png" alt="&quot;Performance Analyzer&quot;" /></p><p>Each category in the timeline describes a thread in the application. Move the cursor on an event (5) on a row to see how long it takes and which function in the source it represents. To display the information only when an event is selected, disable the <b>View Event Information on Mouseover</b> button (4).</p>
<p>The outline (9) summarizes the period for which data was collected. Drag the zoom range (7) or click the outline to move on the outline. You can also move between events by selecting the <b>Jump to Previous Event</b> and <b>Jump to Next Event</b> buttons (1).</p>
<p>Select the <b>Show Zoom Slider</b> button (2) to open a slider that you can use to set the zoom level. You can also drag the zoom handles (8). To reset the default zoom level, right-click the timeline to open the context menu, and select <b>Reset Zoom</b>.</p>
<a name="selecting-event-ranges"></a>
<h3 id="selecting-event-ranges">Selecting Event Ranges</h3>
<p>You can select an event range (6) to view the time it represents or to zoom into a specific region of the trace. Select the <b>Select Range</b> button (3) to activate the selection tool. Then click in the timeline to specify the beginning of the event range. Drag the selection handle to define the end of the range.</p>
<p>You can use event ranges also to measure delays between two subsequent events. Place a range between the end of the first event and the beginning of the second event. The <b>Duration</b> field displays the delay between the events in milliseconds.</p>
<p>To zoom into an event range, double-click it.</p>
<p>To remove an event range, close the <b>Selection</b> dialog.</p>
<a name="understanding-the-data"></a>
<h3 id="understanding-the-data">Understanding the Data</h3>
<p>Generally, events in the timeline view indicate how long a function call took. Move the mouse over them to see details. The details always include the address of the function, the approximate duration of the call, the ELF file the function resides in, the number of samples collected with this function call active, the total number of times this function was encountered in the thread, and the number of samples this function was encountered in at least once.</p>
<p>For functions with debug information available, the details include the location in source code and the name of the function. You can click on such events to move the cursor in the code editor to the part of the code the event is associated with.</p>
<p>As the Perf tool only provides periodic samples, the Performance Analyzer cannot determine the exact time when a function was called or when it returned. You can, however, see exactly when a sample was taken in the second row of each thread. The Performance Analyzer assumes that if the same function is present at the same place in the call chain in multiple consecutive samples, then this represents a single call to the respective function. This is, of course, a simplification. Also, there may be other functions being called between the samples taken, which do not show up in the profile data. However, statistically, the data is likely to show the functions that spend the most CPU time most prominently.</p>
<p>If a function without debug information is encountered, further unwinding of the stack may fail. Unwinding will also fail for some symbols implemented in assembly language. If unwinding fails, only a part of the call chain is displayed, and the surrounding functions may seem to be interrupted. This does not necessarily mean they were actually interrupted during the execution of the application, but only that they could not be found in the stacks where the unwinding failed.</p>
<p>JavaScript functions from the QML engine running in the JIT mode can be unwound. However, their names will only be displayed when <code>QV4_PROFILE_WRITE_PERF_MAP</code> is set. Compiled JavaScript generated by the <a href="http://doc.qt.io/QtQuickCompiler/">Qt Quick Compiler</a> can also be unwound. In this case the C++ names generated by the compiler are shown for JavaScript functions, rather than their JavaScript names. When running in interpreted mode, stack frames involving QML can also be unwound, showing the interpreter itself, rather than the interpreted JavaScript.</p>
<p>Kernel functions included in call chains are shown on the third row of each thread.</p>
<p>The coloring of the events represents the actual sample rate for the specific thread they belong to, across their duration. The Linux kernel will only take a sample of a thread if the thread is active. At the same time, the kernel tries to honor the requested event period. Thus, differences in the sampling frequency between different threads indicate that the thread with more samples taken is more likely to be the overall bottleneck, and the thread with less samples taken has likely spent time waiting for external events such as I/O or a mutex.</p>
<a name="viewing-statistics"></a>
<h2 id="viewing-statistics">Viewing Statistics</h2>
<p class="centerAlign"><img src="images/qtcreator-performance-analyzer-statistics.png" alt="" /></p><p>The <b>Statistics</b> view displays the number of samples each function in the timeline was contained in, in total and when on the top of the stack (called <code>self</code>). This allows you to examine which functions you need to optimize. A high number of occurrences might indicate that a function is triggered unnecessarily or takes very long to execute.</p>
<p>Click on a row to move to the respective function in the source code in the code editor.</p>
<p>The <b>Callers</b> and <b>Callees</b> panes show dependencies between functions. They allow you to examine the internal functions of the application. The <b>Callers</b> pane summarizes the functions that called the function selected in the main view. The <b>Callees</b> pane summarizes the functions called from the function selected in the main view.</p>
<p>Click on a row to move to the respective function in the source code in the code editor and select it in the main view.</p>
<p>To copy the contents of one view or row to the clipboard, select <b>Copy Table</b> or <b>Copy Row</b> in the context menu.</p>
<a name="visualizing-statistics-as-flame-graphs"></a>
<h3 id="visualizing-statistics-as-flame-graphs">Visualizing Statistics as Flame Graphs</h3>
<p class="centerAlign"><img src="images/qtcreator-performance-analyzer-flamegraph.png" alt="" /></p><p>The <b>Flame Graph</b> view shows a more concise statistical overview of the execution. The horizontal bars show an aspect of the samples taken for a certain function, relative to the same aspect of all samples together. The nesting shows which functions were called by which other ones.</p>
<p>The <b>Visualize</b> button lets you choose what aspect to show in the <b>Flame Graph</b>.</p>
<ul>
<li><b>Samples</b> is the default visualization. The size of the horizontal bars represents the number of samples recorded for the given function.</li>
<li>In <b>Peak Usage</b> mode, the size of the horizontal bars represents the amount of memory allocated by the respective functions, at the point in time when the allocation's memory usage was at its peak.</li>
<li>In <b>Allocations</b> mode, the size of the horizontal bars represents the number of memory allocations triggered by the respective functions.</li>
<li>In <b>Releases</b> mode, the size of the horizontal bars represents the number of memory releases triggered by the respective functions.</li>
</ul>
<p>The <b>Peak Usage</b>, <b>Allocations</b>, and <b>Releases</b> modes will only show any data if samples from memory trace points have been recorded.</p>
<a name="interaction-between-the-views"></a>
<h3 id="interaction-between-the-views">Interaction between the views</h3>
<p>When you select a stack frame in either of the <b>Timeline</b>, <b>Flame Graph</b>, or <b>Statistics</b> views, information about it is displayed in the other two views. To view a time range in the <b>Statistics</b> and <b>Flame Graph</b> views, select <b>Analyze</b> &gt; <b>Performance Analyzer Options</b> &gt; <b>Limit to the Range Selected in Timeline</b>. To show the full stack frame, select <b>Show Full Range</b>.</p>
<a name="loading-perf-data-files"></a>
<h2 id="loading-perf-data-files">Loading Perf Data Files</h2>
<p>You can load any <code>perf.data</code> files generated by recent versions of the Linux Perf tool and view them in Qt Creator. Select <b>Analyze</b> &gt; <b>Performance Analyzer Options</b> &gt; <b>Load perf.data</b> to load a file.</p>
<p class="centerAlign"><img src="images/qtcreator-cpu-usage-analyzer-load-perf-trace.png" alt="" /></p><p>The Performance Analyzer needs to know the context in which the data was recorded to find the debug symbols. Therefore, you have to specify the kit that the application was built with and the folder where the application executable is located.</p>
<p>The Perf data files are generated by calling <code>perf record</code>. Make sure to generate call graphs when recording data by starting Perf with the <code>--call-graph</code> option. Also check that the necessary debug symbols are available to the Performance Analyzer, either at a standard location (<code>/usr/lib/debug</code> or next to the binaries), or as part of the Qt package you are using.</p>
<p>The Performance Analyzer can read Perf data files generated in either frame pointer or dwarf mode. However, to generate the files correctly, numerous preconditions have to be met. All system images for the <a href="http://doc.qt.io/QtForDeviceCreation/qtee-supported-platforms.html">Qt for Device Creation reference devices</a>, except for Freescale iMX53 Quick Start Board and SILICA Architect Tibidabo, are correctly set up for profiling in the dwarf mode. For other devices, check whether Perf can read back its own data in a sensible way by checking the output of <code>perf report</code> or <code>perf script</code> for the recorded Perf data files.</p>
<a name="loading-and-saving-trace-files"></a>
<h2 id="loading-and-saving-trace-files">Loading and Saving Trace Files</h2>
<p>You can save and load trace data in a format specific to the Performance Analyzer with the respective entries in <b>Analyze</b> &gt; <b>Performance Analyzer Options</b>. This format is self-contained, and therefore loading it does not require you to specify the recording environment. You can transfer such trace files to a different computer without any tool chain or debug symbols and analyze them there.</p>
<a name="troubleshooting"></a>
<h2 id="troubleshooting">Troubleshooting</h2>
<p>The Performance Analyzer might fail to record data for the following reasons:</p>
<ol class="1" type="1"><li>Perf events may be globally disabled on your system. The preconfigured Boot to Qt images come with perf events enabled. For a custom configuration you need to make sure that the file <code>/proc/sys/kernel/perf_event_paranoid</code> contains a value smaller than <code>2</code>. For maximum flexibility in recording traces you can set the value to <code>-1</code>. This allows any user to record any kind of trace, even using raw kernel trace points.</li>
<li>The connection between the target device and the host may not be fast enough to transfer the data produced by Perf. Try modifying the values of the <b>Stack snapshot size</b> or <b>Sample period</b> settings.</li>
<li>Perf may be buffering the data forever, never sending it. Add <code>--no-delay</code> or <code>--no-buffering</code> to the <b>Additional arguments</b> field.</li>
<li>Some versions of Perf will not start recording unless given a certain minimum sampling frequency. Try with a <b>Sample period</b> value of 1000.</li>
<li>On some devices, in particular various i.MX6 Boards, the hardware performance counters are dysfunctional and the Linux kernel may randomly fail to record data after some time. Perf can use different types of events to trigger samples. You can get a list of available event types by running <code>perf list</code> on the device and then choose the respective event types in the settings. The choice of event type affects the performance and stability of the sampling. The <code>cpu-clock</code> <code>software</code> event is a safe but relatively slow option as it does not use the hardware performance counters, but drives the sampling from software. After the sampling has failed, reboot the device. The kernel may have disabled important parts of the performance counters system.</li>
</ol>
<p>Output from the helper program that processes the data is displayed in the <b>General Messages</b> output pane.</p>
</div>
<!-- @@@creator-cpu-usage-analyzer.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="creator-heob.html">Detecting Memory Leaks with Heob</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="creator-cppcheck.html">Analyzing Code with Cppcheck</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
